<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="铁芯粗" prop="coreDiameter">
        <el-input
          v-model="queryParams.coreDiameter"
          placeholder="请输入铁芯粗"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总长" prop="totalLength">
        <el-input
          v-model="queryParams.totalLength"
          placeholder="请输入总长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="轴承距离" prop="bearingDistance">
        <el-input
          v-model="queryParams.bearingDistance"
          placeholder="请输入轴承距离"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="前轴承位" prop="frontBearing">
        <el-input
          v-model="queryParams.frontBearing"
          placeholder="请输入前轴承位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="后轴承位" prop="rearBearing">
        <el-input
          v-model="queryParams.rearBearing"
          placeholder="请输入后轴承位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="螺丝" prop="screw">
        <el-input
          v-model="queryParams.screw"
          placeholder="请输入螺丝"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="齿" prop="teeth">
        <el-input
          v-model="queryParams.teeth"
          placeholder="请输入齿"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="键槽 (是否有键槽)" prop="keyway">
        <el-input
          v-model="queryParams.keyway"
          placeholder="请输入键槽 (是否有键槽)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡簧 (是否有卡簧)" prop="circlip">
        <el-input
          v-model="queryParams.circlip"
          placeholder="请输入卡簧 (是否有卡簧)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:shaftspecifications:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:shaftspecifications:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:shaftspecifications:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:shaftspecifications:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shaftspecificationsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="编号" align="center" prop="code" />
      <el-table-column label="审核状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 2" type="success">已审核</el-tag>
          <el-tag v-else type="danger">未审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.image"
            :src="getImageUrl(scope.row.image)"
            style="width: 60px; height: 60px"
            :preview-src-list="scope.row.image ? [getImageUrl(scope.row.image)] : []">
          </el-image>
          <span v-else>无图片</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="category" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="铁芯粗" align="center" prop="coreDiameter" />
      <el-table-column label="总长" align="center" prop="totalLength" />
      <el-table-column label="轴承距离" align="center" prop="bearingDistance" />
      <el-table-column label="前轴承位" align="center" prop="frontBearing" />
      <el-table-column label="后轴承位" align="center" prop="rearBearing" />
      <el-table-column label="螺丝" align="center" prop="screw" />
      <el-table-column label="齿" align="center" prop="teeth" />
      <el-table-column label="正反齿" align="center" prop="gearType" />
      <el-table-column label="键槽 (是否有键槽)" align="center" prop="keyway" />
      <el-table-column label="卡簧 (是否有卡簧)" align="center" prop="circlip" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 设置审核状态 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAudit(scope.row)"
            v-hasPermi="['business:shaftspecifications:audit']"
          >审核</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:shaftspecifications:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:shaftspecifications:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改转子查询对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="code">
          <el-input v-model="form.code" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="审核状态" prop="state" >
          <el-input v-model="form.state" disabled />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image" :limit="1" />
          <div class="el-upload__tip" style="color: #f56c6c;">注意：系统将只保存图片文件名，不保存完整路径</div>
        </el-form-item>
        <el-form-item label="分类" prop="category">
          <el-input v-model="form.category" placeholder="请输入分类" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="铁芯粗" prop="coreDiameter">
          <el-input v-model="form.coreDiameter" placeholder="请输入铁芯粗" />
        </el-form-item>
        <el-form-item label="总长" prop="totalLength">
          <el-input v-model="form.totalLength" placeholder="请输入总长" />
        </el-form-item>
        <el-form-item label="轴承距离" prop="bearingDistance">
          <el-input v-model="form.bearingDistance" placeholder="请输入轴承距离" />
        </el-form-item>
        <el-form-item label="前轴承位" prop="frontBearing">
          <el-input v-model="form.frontBearing" placeholder="请输入前轴承位" />
        </el-form-item>
        <el-form-item label="后轴承位" prop="rearBearing">
          <el-input v-model="form.rearBearing" placeholder="请输入后轴承位" />
        </el-form-item>
        <el-form-item label="螺丝" prop="screw">
          <el-input v-model="form.screw" placeholder="请输入螺丝" />
        </el-form-item>
        <el-form-item label="齿" prop="teeth">
          <el-input v-model="form.teeth" placeholder="请输入齿" />
        </el-form-item>
        <el-form-item label="键槽 (是否有键槽)" prop="keyway">
          <el-input v-model="form.keyway" placeholder="请输入键槽 (是否有键槽)" />
        </el-form-item>
        <el-form-item label="卡簧 (是否有卡簧)" prop="circlip">
          <el-input v-model="form.circlip" placeholder="请输入卡簧 (是否有卡簧)" />
        </el-form-item>
<!--        <el-form-item label="删除标志" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShaftspecifications, getShaftspecifications, delShaftspecifications, addShaftspecifications, updateShaftspecifications } from '@/api/business/shaftspecifications';
import ImageUpload from '@/components/ImageUpload';

export default {
  name: 'Shaftspecifications',
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 转子查询表格数据
      shaftspecificationsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        category: null,
        brand: null,
        model: null,
        coreDiameter: null,
        totalLength: null,
        bearingDistance: null,
        frontBearing: null,
        rearBearing: null,
        screw: null,
        teeth: null,
        gearType: null,
        keyway: null,
        circlip: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: '编号不能为空', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '分类不能为空', trigger: 'blur' }
        ],
        brand: [
          { required: true, message: '品牌不能为空', trigger: 'blur' }
        ],
        model: [
          { required: true, message: '型号不能为空', trigger: 'blur' }
        ]
      },
      
      // 图片上传基础路径
      baseImageUrl: process.env.VUE_APP_BASE_API + '/profile/upload/',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询转子查询列表 */
    getList() {
      this.loading = true;
      listShaftspecifications(this.queryParams).then(response => {
        this.shaftspecificationsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        state: null,
        category: null,
        brand: null,
        model: null,
        coreDiameter: null,
        totalLength: null,
        bearingDistance: null,
        frontBearing: null,
        rearBearing: null,
        screw: null,
        teeth: null,
        gearType: null,
        keyway: null,
        circlip: null,
        image: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加转子查询';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getShaftspecifications(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改转子查询';
      });
    },
    handleAudit(row) {
      // 弹窗提示是否确认
      this.$modal.confirm('是否确认审核转子查询编号为"' + row.id + '"的数据项？').then(function() {
        return updateShaftspecifications({ ...row, state: 2 });
      }).then(() => {
        this.$modal.msgSuccess('审核成功');
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 确保图片字段只保存文件名
          if (this.form.image && this.form.image.includes('/')) {
            const imageParts = this.form.image.split('/');
            this.form.image = imageParts[imageParts.length - 1];
          }
          
          if (this.form.id != null) {
            updateShaftspecifications(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addShaftspecifications(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除转子查询编号为"' + ids + '"的数据项？').then(function() {
        return delShaftspecifications(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/shaftspecifications/export', {
        ...this.queryParams
      }, `shaftspecifications_${new Date().getTime()}.xlsx`);
    },
    
    // 获取图片完整URL
    getImageUrl(imagePath) {
      if (!imagePath) {
        return '';
      }
      
      // 如果已经是完整路径，直接返回
      if (imagePath.includes('http://') || imagePath.includes('https://')) {
        return imagePath;
      }
      
      // 如果是相对路径但带有/profile/，直接拼接基础URL
      if (imagePath.startsWith('/profile/')) {
        return process.env.VUE_APP_BASE_API + imagePath;
      }
      
      // 否则假设是文件名，拼接完整路径
      // 尝试使用当前日期格式构建路径
      const now = new Date();
      const year = now.getFullYear();
      const month = String(now.getMonth() + 1).padStart(2, '0');
      const day = String(now.getDate()).padStart(2, '0');
      
      // 首先尝试使用当前日期路径
      const currentPath = `/profile/upload/${year}/${month}/${day}/${imagePath}`;
      
      return process.env.VUE_APP_BASE_API + currentPath;
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          // 确保图片字段只保存文件名
          if (this.form.image && this.form.image.includes('/')) {
            const imageParts = this.form.image.split('/');
            this.form.image = imageParts[imageParts.length - 1];
          }
          
          if (this.form.id != null) {
            updateShaftspecifications(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addShaftspecifications(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>

<style>
/* The avatar uploader styles are no longer needed since we're using the ImageUpload component */
</style>
